//
//  SVisitorView.swift
//  SWeiboBoom
//
//  Created by Angus.丶 ShangYang on 16/8/9.
//  Copyright © 2016年 Angus_Shyang. All rights reserved.
//

import UIKit
import SnapKit

class SVisitorView: UIView {

    //声明一个闭包
    var closure:(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //提供一个监听方法
    func buttonClick(){
        
        //执行闭包 03
        closure?()
    }
    
    //MARK: - 供外界调用的方法
    func setupVisitorViewInfo(imgName:String?,title:String?){
        //代表设置的是消息/发现/我页面
        if let img = imgName,let tit = title{
            iconImageView.image = UIImage(named: img)
            messageLabel.text = tit
            //隐藏圆圈
            circleImageView.hidden = true
        }else{
            //代表为首页
            setupAnimal()
        }
    }
    
 
    //MARK:设置圆圈动画
    func setupAnimal(){
        let animal = CABasicAnimation(keyPath:"transform.rotation")
        //设置toValue(转动多少度)
        animal.toValue = M_PI * 2
        //设置动画时长
        animal.duration = 10
        //设置循环次数
        animal.repeatCount = MAXFLOAT
        //如果用户切换子控制器或者退到后台  默认动画移除
        animal.removedOnCompletion = false
        //添加动画
        circleImageView.layer.addAnimation(animal, forKey: nil)
    }
    
    //MARK: - 设置视图
    private func setupUI(){
        backgroundColor = RGB(237, green: 237, blue: 237)
        //1.添加控件
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        //2.添加约束
        //2.1 圆圈imageView
        circleImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        //2.2 遮挡图片
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        //2.3 主图片
        iconImageView.snp_makeConstraints{ (make) -> Void in
            make.center.equalTo(self)
        }
        //2.4 文字
        messageLabel.snp_makeConstraints{ (make) -> Void in
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.height.equalTo(36)
            make.centerX.equalTo(self)
        }
        //2.5 注册按钮
        registerButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100,height: 36))
        }
        //2.6 登录按钮
        loginButton.snp_makeConstraints { (make) -> Void in
            make.size.top.equalTo(registerButton)
            make.trailing.equalTo(messageLabel)
        }
    }
    
    
    //MARK:懒加载控件
    //圆圈
    private lazy var circleImageView:UIImageView = UIImageView(imgName:"visitordiscover_feed_image_smallicon")
    //遮挡
    private lazy var maskImageView:UIImageView = UIImageView(imgName:"visitordiscover_feed_mask_smallicon")
    //房子
    private lazy var iconImageView:UIImageView = UIImageView(imgName:"visitordiscover_feed_image_house")
    //文字
    private lazy var messageLabel:UILabel = {
        let lab = UILabel()
        lab.textColor = AppThemeColor
        lab.font = UIFont.systemFontOfSize(15)
        lab.numberOfLines = 0
        lab.textAlignment = .Center
        lab.text = "关注一些人,回到这里看看有设么惊喜关注一些人,回到这里看看有什么惊喜"
        //设置最大显示宽度
        lab.preferredMaxLayoutWidth = 240
        return lab
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
    button.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(AppThemeColor, forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        return button
    }()
    
    //登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action:"buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
    button.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(AppThemeColor, forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        return button
    }()
}










